<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Stock_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据分页信息返回库存购买记录
	 */
	public function find_all_stocks($per_page,$offset,$name){
		$this->db->select('t_stock.id,t_user.shopname,t_product.name,t_product.stock,t_stock.amount,t_stock.date');
		$this->db->from('t_stock');
		$this->db->join('t_product', 't_stock.productid = t_product.id','left');
		$this->db->join('t_user', 't_stock.userid = t_user.id','left');
		$this->db->like('t_product.name', $name);
		$this->db->limit($per_page, $offset);
		$this->db->order_by("t_stock.id", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 返回库存购买记录总数
	 */
	public function count_stocks($name){
		$this->db->join('t_product', 't_stock.productid = t_product.id','left');
		$this->db->join('t_user', 't_stock.userid = t_user.id','left');
		$this->db->like('t_product.name', $name);
		$query = $this->db->get('t_stock');
		return $query->num_rows();
	}
}